<?php
include('database.php');

$email = addslashes(@$_GET['email']);
$brand = addslashes(@$_GET['brand']);
$token = addslashes(@$_GET['token']);

if(!empty($email) && !empty($brand) && !empty($token)) {
    $data = checkEmail($email);
    if(!$data['email']) {
        $sql = "INSERT INTO  `cp_users`(`email`, `brand`, `token`) VALUES ('".$email."', '".$brand."', '".$token."');";
        $result = mysql_query($sql);

        if (!$result) {
            //log and send email alert
            //echo "Could not successfully run query ($sql) from DB: " . mysql_error();
            echo '{"result":"false", "msg":"System Error"}';exit;
        }
        

    } else {
        if($data['status']) {
            echo '{"result":"true", "msg":"User has active"}';exit;
        } else {
            echo '{"result":"false", "msg":"User is exits. Please wait in 2 working day"}';exit;
        }
    }
    
    
} else {
    echo '{"result":"false", "msg":"Please input full your info"}';exit;
}

echo '{"result":"true", "msg":"You are sign up successful. Please wait in 2 working day "}';exit;


/**
* Check email is exits in database 
* @param string $email is email of user
* @return boolean true|false
*/
function checkEmail($email)
{
    $query = 'SELECT * FROM `cp_users` WHERE email ="' . $email . '"';
    $result = mysql_query($query);
    $data = array();
    if ($result) {
        $data = mysql_fetch_assoc($result);
   }

    return $data;
}